package misc

import (
    "strings"
	"errors"
    "dhfshop/models"
	"github.com/astaxie/beego/orm"
)

//海关商品编码
type Hscode struct {
	HsId               int64   `orm:"column(hs_id);pk" json:"hs_id" form:"hs_id"`     //HS编码
	Name               string  `orm:"column(name)" json:"name" form:"name"`        //名称
	Element            string  `orm:"column(element)" json:"element" form:"element"` //海关申报要素 
	Factor             string  `orm:"column(factor)" json:"factor" form:"factor"`    //海关监管条件 
    GeneralTariff      string  `orm:"column(general_tariff)" json:"general_tariff" form:"general_tariff"`    //普通税率 
	PreferentialTariff string `orm:"column(preferential_tariff)" json:"preferential_tariff" form:"preferential_tariff"`     //优惠税率
	CheckCat         string  `orm:"column(check_cat)" json:"check_cat" form:"check_cat"`    //检验检疫类别
	Remark           string  `orm:"column(remark)" json:"remark" form:"remark"`    //海关监管条件
	ExportTaxRate    string  `orm:"column(export_tax_rate)" json:"export_tax_rate" form:"export_tax_rate"`    //出口税率
	ConsumptionTaxRate string `orm:"column(consumption_tax_rate)" json:"consumption_tax_rate" form:"consumption_tax_rate"`    //消费税率
	VatRate           string  `orm:"column(vat_rate)" json:"vat_rate" form:"vat_rate"`    //增值税率
	Unit1             string  `orm:"column(unit_1)" json:"unit_1" form:"unit_1"`    //第一法定单位
	Unit2             string  `orm:"column(unit_2)" json:"unit_2" form:"unit_2"`    //第一法定单位
}

func init() {
    orm.RegisterModel(new(Hscode))   
}

func AddHscode(m *Hscode) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func AddMultiHscodes(list []*Hscode) (err error) {
    o := models.GetDefaultDB()
    size := len(list)
	if size > 1 {
	    _, err = o.InsertMulti(size, list) 
	}else{
	   _, err = o.Insert(list[0])
	}
	return
}

func GetHscodeById(id int64) (v *Hscode, err error) {
	o := models.GetDefaultDB()
	v = &Hscode{HsId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}


func GetAllHscodes(query map[string]string, fields []string, order_by map[string]string, 
    offset int, limit int) ([]*Hscode, int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(Hscode))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	cnt, err := qs.Count()
	// order by:
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*Hscode
	if _, err := qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		return lists, cnt, nil
	}
	return nil, 0, err
}

func UpdateHscodeById(m *Hscode, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := Hscode{HsId: m.HsId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}

func DeleteHscode(id int64) (err error) {
	o := models.GetDefaultDB()
	v := Hscode{HsId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}

